package com.lzc.zhxy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzc.zhxy.mapper.TbTeacherMapper;

import com.lzc.zhxy.pojo.LoginForm;
import com.lzc.zhxy.pojo.TbTeacher;
import com.lzc.zhxy.service.TbTeacherService;
import com.lzc.zhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
* @author 82389
* @description 针对表【tb_teacher】的数据库操作Service实现
* @createDate 2022-04-16 18:00:15
*/
@Service("tbTeacherService")
@Transactional
public class TbTeacherServiceImpl extends ServiceImpl<TbTeacherMapper, TbTeacher> implements TbTeacherService {

    /**
     * 登陆方法
     * @param loginForm
     * @return
     */
    @Override
    public TbTeacher login(LoginForm loginForm) {
        QueryWrapper<TbTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));

        TbTeacher tbTeacher = baseMapper.selectOne(queryWrapper);
        return tbTeacher;
    }

    /**
     * 根据用户id查找用户信息
     * @param intValue
     * @return
     */
    @Override
    public TbTeacher getTeacherById(int intValue) {
        QueryWrapper<TbTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",intValue);
        TbTeacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    /**
     * 获取教师信息,分页带条件
     * @param page
     * @param teacher
     * @return
     */
    @Override
    public IPage<TbTeacher> getTeachersByOpr(Page<TbTeacher> page, TbTeacher teacher) {
        QueryWrapper<TbTeacher> queryWrapper = new QueryWrapper<>();
        if (teacher!=null){
            String clazzName = teacher.getClazzName();
            if (!StringUtils.isEmpty(clazzName)){
                queryWrapper.eq("clazz_name",clazzName);
            }
            //教师名称条件
            if (!StringUtils.isEmpty(teacher.getName())){
                queryWrapper.like("name",teacher.getName());
            }
            queryWrapper.orderByDesc("id");
            queryWrapper.orderByAsc("name");
        }
        IPage<TbTeacher> selectPage = baseMapper.selectPage(page, queryWrapper);
        return selectPage;
    }
}
